package com.borya.dao.impl;

import com.borya.dao.AppConfDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.framework.util.ObjectUtils;
import com.borya.model.db.AppTypeInfo;
import com.borya.model.enumcation.OtherConfType;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("appConfDAOImpl")
public class AppConfDAOImpl extends BaseDAOImpl implements AppConfDAO {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public List<String> getAll(OtherConfType confType) {
        try(ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select detail_type from tf_app_type");
            List<String> detailTypes = session.executeQuery(sb.toString(),rs->{
                List<String > types = new ArrayList<>();
                if(rs != null){
                   while(rs.next()){
                       types.add(rs.getString(1));
                   }
               }
               return types;
            });
            if(ObjectUtils.isNullOrEmpty(detailTypes)){
                return new ArrayList<>();
            }
            List<String> dealerIds = new ArrayList<>();
            for(String detailType:detailTypes){
                sb.delete(0,sb.length());
                sb.append("select conf_value from tf_app_other_conf_").append(detailType);
                sb.append(" where conf_type=? and conf_name=? ");
                session.executeQuery(sb.toString(),rs->{
                    if(rs != null){
                        while (rs.next()){
                            dealerIds.add(rs.getString(1));
                        }
                    }
                    return null;
                } ,confType.getConfType(),confType.getConfName());
            }
            return dealerIds;
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public List<String> getConfValues(OtherConfType confType, int detailType) {
        try(ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select conf_value from tf_app_other_conf_").append(detailType);
            sb.append(" where conf_type=? and conf_name=? ");
            return session.executeQuery(sb.toString(),rs->{
                if(rs != null){
                    List<String> dealerIds = new ArrayList<>();
                    while (rs.next()){
                        dealerIds.add(rs.getString(1));
                    }
                    return dealerIds;
                }
                return null;
            } ,confType.getConfType(),confType.getConfName());
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public Map<String, String> getConfValuesResMap(OtherConfType confType, int detailType) {
        try(ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select conf_name,conf_value from tf_app_other_conf_").append(detailType);
            sb.append(" where conf_type=? ");
            return session.executeQuery(sb.toString(),rs->{
                if(rs != null){
                    Map<String, String> maps = new HashMap<>();
                    while (rs.next()){
                        maps.put(rs.getString(1),rs.getString(2));
                    }
                    return maps;
                }
                return null;
            } ,confType.getConfType());
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public Map<String, String> getAllTables(int detailType) {
        try(ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select default_table,self_table from tf_app_table_").append(detailType);
            return session.executeQuery(sb.toString(),rs->{
                if(rs != null){
                    Map<String, String> maps = new HashMap<>();
                    while (rs.next()){
                        maps.put(rs.getString(1),rs.getString(2));
                    }
                    return maps;
                }
                return null;
            });
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    @Override
    public AppTypeInfo getAppInfo(int detailType) {
        try(ServerSqlSession session = getSqlDataSource().session();){
            StringBuilder sb = new StringBuilder(128);
            sb.append("select detail_type,msg_name,package_name from tf_app_type where detail_type=? limit 1");
            return session.executeQuery(sb.toString(),rs->{
                if(rs != null && rs.next()){
                    AppTypeInfo appInfo = new AppTypeInfo();
                    appInfo.setDetailType(rs.getInt(1));
                    appInfo.setMsgName(rs.getString(2));
                    appInfo.setPackageName(rs.getString(3));
                    return appInfo;
                }
                return null;
            },detailType);
        } catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }
}
